﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace BigPixels
{
    public partial class ToolsForm : Form
    {
        public event EventHandler<ColourSelectEventArgs> PaintColourSelected;
        public event EventHandler<ColourSelectEventArgs> CanvasColourSelected;

        public ToolsForm()
        {
            InitializeComponent();

            this.colourPot1.Colour = Color.Black;
            this.colourPot2.Colour = Color.White;
        }

        private void ToolsForm_Load(object sender, EventArgs e)
        {
            var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "palette.xml");

            var colours = BigPixelStorage.LoadPalette(path);

            this.palette1.SetColours(colours);
            this.palette1.PaintColourSelected += new EventHandler<ColourSelectEventArgs>(palette1_PaintColourSelected);
            this.palette1.CanvasColourSelected += new EventHandler<ColourSelectEventArgs>(palette1_CanvasColourSelected);
        }

        void palette1_PaintColourSelected(object sender, ColourSelectEventArgs e)
        {
            this.PaintColour = e.Value;

            var changed = this.PaintColourSelected;

            if (changed != null)
                changed(this, new ColourSelectEventArgs(e.Value));
        }

        void palette1_CanvasColourSelected(object sender, ColourSelectEventArgs e)
        {
            this.CanvasColour = e.Value;

            var changed = this.CanvasColourSelected;

            if (changed != null)
                changed(this, new ColourSelectEventArgs(e.Value));
        }


        public Color PaintColour
        {
            get
            {
                return this.colourPot1.Colour;
            }
            set
            {
                this.colourPot1.Colour = value;
            }
        }

        public Color CanvasColour
        {
            get
            {
                return this.colourPot2.Colour;
            }
            set
            {
                this.colourPot2.Colour = value;
            }
        }

        public void ImportColours(List<PixelValue> newColours)
        {
            this.palette1.SetColours(newColours);
        }

        private void ToolsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "palette.xml");

            var colours = this.palette1.GetColours();
            BigPixelStorage.SavePalette(path, colours);
        }
    }
}
